//
//  LS_EmoticonKeyboardView.swift
//  EmoticonKeyboard
//
//  Created by 罗李苏 on 16/5/30.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SnapKit

private let emoticonCellId = "emoticonCellId"
private let toolBarHeight: CGFloat = 37
private let keyboardViewHeight: CGFloat = 220
class LS_EmoticonKeyboardView: UIView {
    override init(frame: CGRect) {
        let rect = CGRect(x: 0, y: 0, width: 0, height: keyboardViewHeight)
        super.init(frame: rect)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置UI界面
    private func setupUI(){
    //设置背景色
        backgroundColor = UIColor.yellowColor()
        addSubview(toolBar)
        //设置约束
        toolBar.snp_makeConstraints { (make) in
            make.left.bottom.right.equalTo(self)
            //设置高度
            make.height.equalTo(toolBarHeight)
        }
        addSubview(collectionView)
        collectionView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        //MARK:实现toolBar的闭包
        toolBar.selectedIndexCallBack = {(index:Int) -> () in
            //执行这个闭包 是让点击toolbBar 上的按钮 让 collectionView 滚动到响应的位置
            let indexPath = NSIndexPath(forRow: 0, inSection: index)
            //滚动
            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
        }
    }
    //显示表情的子视图
    private lazy var collectionView: UICollectionView = {
        //流水布局
        let layout = UICollectionViewFlowLayout()
        //设置cell的滑动方向
        layout.scrollDirection = .Horizontal
        //设置cell的间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSizeMake(UIScreen.mainScreen().bounds.width, keyboardViewHeight - toolBarHeight)
        let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        cv.dataSource = self
        //注册cell
        cv.registerClass(LS_EmoticonPageCell.self, forCellWithReuseIdentifier: emoticonCellId)
        //设置分页效果
        cv.pagingEnabled = true
        //取消弹簧效果
        cv.bounces = false
        return cv
    }()
    //懒加载底部的toolbar
    private lazy var toolBar:LS_EmoticonToolBar = LS_EmoticonToolBar()
   }
//MARK: collectionView 的数据源方法
extension LS_EmoticonKeyboardView: UICollectionViewDataSource{
    
    //多少组
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 3
    }
    //每组有几个cell
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    //显示的内容
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(emoticonCellId, forIndexPath: indexPath) as! LS_EmoticonPageCell
        cell.backgroundColor = randomColor()
        cell.indexPath = indexPath
        return cell
    }

}
